package mt;

import base.jfinal.plugin.ReadSql;
import mt.business.Cache;
import org.apache.log4j.Logger;

import base.jfinal.RoutesUtil;
import base.jfinal.ServletHandler;
import base.jfinal.plugin.AutoTableBindPlugin;
import base.jfinal.plugin.OrderContainerFactory;
import base.jfinal.plugin.TableNameStyle;
import base.util.PropertiesContent;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.FakeStaticHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.render.ViewType;


public class WebConfig extends JFinalConfig {
	private Logger log=Logger.getLogger(getClass());
	public static DruidPlugin druidPlugin;
	/**
	 * 配置常量O
	 */
	@Override
	public void configConstant(Constants me){

		me.setDevMode(PropertiesContent.getToBool("jfinal.devmode",false));
		me.setViewType(ViewType.OTHER);

		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath() +"/upload");
		me.setMaxPostSize(PropertiesContent.getToInteger("jfinal.maxpostsize",104857600));//100mb

		me.setError500View("/err.html");
	}
	
	@Override
	public void configRoute(Routes me) {
		RoutesUtil.add(me);
		log.debug("====configRoute====");
	}

	@Override
	public void configPlugin(Plugins me) {
		//me.add(new EhCachePlugin());
		// 配置数据库连接池插件
		druidPlugin = new DruidPlugin(PropertiesContent.get("url"),
									  PropertiesContent.get("username"),
									  PropertiesContent.get("password"),
									  PropertiesContent.get("driver"));
		druidPlugin.set(PropertiesContent.getToInteger("initialSize", 1),
				PropertiesContent.getToInteger("minIdle", 1),
				PropertiesContent.getToInteger("maxActive", 1));
//		druidPlugin.setMaxWait(PropertiesContent.getToLong("maxWait",30000L));
//		druidPlugin.setTimeBetweenEvictionRunsMillis(PropertiesContent.getToLong("minEvictableIdleTimeMillis",60000L));
//		druidPlugin.setValidationQuery(PropertiesContent.get("validationQuery","SELECT 'x'"));
//		druidPlugin.setTestWhileIdle(PropertiesContent.getToBool("testWhileIdle",true));
		druidPlugin.addFilter(new StatFilter());
		druidPlugin.start();
		me.add(druidPlugin);
		
		//添加自动绑定model与表插件
		AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(druidPlugin, TableNameStyle.LOWER);
		autoTableBindPlugin.setShowSql(true);
		autoTableBindPlugin.setDialect(new MysqlDialect());
		autoTableBindPlugin.setContainerFactory(new OrderContainerFactory());
		me.add(autoTableBindPlugin);


	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ServletHandler());//添加处理Servlet的handler
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
	    me.add(dvh);
	    //该处理器将request.getContextPath()存储在root中，可以解决路径问题
	  	ContextPathHandler path = new ContextPathHandler("root");
	  	me.add(path);
	  	me.add(new FakeStaticHandler(".json"));//通过.json后缀访问后台链接
	  	
	}
	
	@Override
	public void afterJFinalStart(){
		ReadSql readSql = new ReadSql();
		readSql.init();
		Cache.Init();
	}
}
